package com.yueke.gemini.controller;

/**
 * created by admin 2020/3/7 19:16
 */

import com.yueke.gemini.context.TenantContextHolder;
import com.yueke.gemini.model.User;
import com.yueke.gemini.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("/login.html")
    public String login() {
        return "/login";
    }

    @PostMapping("/login")
    public String login(@RequestParam(name = "username") String username, @RequestParam(name = "password") String password, ModelMap model) {
        System.out.println("tenant:" + TenantContextHolder.getTenant());
        User user = userService.findByUsername(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                model.put("user", user);
                return "/index";
            } else {
                return "/login";
            }
        } else {
            return "/login";
        }
    }
}
